<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="真实姓名" prop="trueName">
        <el-input
          v-model="queryParams.trueName"
          placeholder="请输入真实姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:customer:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:customer:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:customer:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="顾客姓名" align="center" key="nickName" prop="nickName" :show-overflow-tooltip="true"/>
      <el-table-column label="顾客账号" align="center" key="userName" prop="userName" :show-overflow-tooltip="true"/>
      <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" width="120"/>
      <el-table-column label="状态" align="center" key="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="会员" align="center" key="isMember" prop="isMember">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_or_no" :value="scope.row.isMember"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:customer:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:customer:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleMember(scope.row)"
            v-hasPermi="['system:customer:join']"
            v-if="scope.row.isMember === '0'"
          >加入会员
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户姓名" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入用户姓名" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item  label="用户账号" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户账号" maxlength="30" />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号码" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idNumber">
              <el-input v-model="form.idNumber" placeholder="请输入身份证号" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus } from '@/api/system/user'

  export default {
    dicts: ['sys_normal_disable', 'sys_user_sex','yes_or_no'],
    name: 'Customer',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 【请填写功能名称】表格数据
        customerList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
          phonenumber: undefined,
          status: undefined,
          deptId: undefined,
          userType: '11'
        },
        // 表单参数
        form: {

        },
        // 表单校验
        rules: {
          userName: [
            { required: true, message: "用户名称不能为空", trigger: "blur" },
            { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
          ],
          nickName: [
            { required: true, message: "用户昵称不能为空", trigger: "blur" }
          ],
          email: [
            {
              type: "email",
              message: "请输入正确的邮箱地址",
              trigger: ["blur", "change"]
            }
          ],
          phonenumber: [
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ],
          email: [
            {
              type: "email",
              message: "请输入正确的邮箱地址",
              trigger: ["blur", "change"]
            }
          ],
          phonenumber: [
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ]
        }
      }
    },
    created() {
      this.getList ()
    },
    methods: {
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true
        listUser (this.queryParams).then (response => {
          this.customerList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset ()
      },
      // 表单重置
      reset() {
        this.form = {
          userId: undefined,
          deptId: undefined,
          userName: undefined,
          nickName: undefined,
          password: undefined,
          phonenumber: undefined,
          email: undefined,
          sex: undefined,
          status: '0',
          remark: undefined,
          postIds: [],
          roleIds: []
        }
        this.resetForm ('form')
      },
      // 用户状态修改
      handleStatusChange(row) {
        let text = row.status === '0'?'启用':'停用'
        this.$modal.confirm ('确认要"' + text + '""' + row.userName + '"用户吗？').then (function() {
          return changeUserStatus (row.userId, row.status)
        }).then (() => {
          this.$modal.msgSuccess (text + '成功')
        }).catch (function() {
          row.status = row.status === '0'?'1':'0'
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList ()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm ('queryForm')
        this.handleQuery ()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map (item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset ()
        this.getTreeselect ()
        getUser ().then (response => {
          this.postOptions = response.posts
          this.roleOptions = response.roles
          this.open = true
          this.title = '添加用户'
          this.form.password = this.initPassword
        })
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset ()
        this.open = true
        this.title = '添加顾客信息'
        this.form.password = '123456'
        this.form.roleIds= ['100']
        this.form.status= '0'
        this.form.userType='11'

      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset ()
        const userId = row.userId || this.ids
        getUser (userId).then (response => {
          this.form = response.data
          this.form.roleIds = response.roleIds
          this.open = true
          this.title = '修改用户'
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs[ 'form' ].validate (valid => {
          if ( valid ) {
            if ( this.form.userId != undefined ) {
              updateUser (this.form).then (response => {
                this.$modal.msgSuccess ('修改成功')
                this.open = false
                this.getList ()
              })
            } else {
              addUser (this.form).then (response => {
                this.$modal.msgSuccess ('新增成功')
                this.open = false
                this.getList ()
              })
            }
          }
        })
      },

      /** 删除按钮操作 */
      handleDelete(row) {
        const userIds = row.userId || this.ids
        this.$modal.confirm ('是否确认删除用户编号为"' + userIds + '"的数据项？').then (function() {
          return delUser (userIds)
        }).then (() => {
          this.getList ()
          this.$modal.msgSuccess ('删除成功')
        }).catch (() => {
        })
      },
      handleMember(rowDate) {
        this.$confirm('加入会员需要缴费100元,一年内可以享受9折优惠 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.form = rowDate
          this.form.roleIds = ['100']
          this.form.isMember ='1'
          updateUser (this.form).then (response => {
            this.$modal.msgSuccess ('已成功加入会员')
            this.open = false
            this.getList ()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消加入会员'
          });
        });

      }
    }
  }
</script>
